package com.vhall.component.controller.room;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.vhall.component.controller.room.facade.RoomConsoleFacade;
import com.vhall.component.entity.audit.vo.AuditRecordResponseVO;
import com.vhall.component.entity.inav.vo.InavRoomGetRespVO;
import com.vhall.component.entity.room.RoomBaseInfoResponseVO;
import com.vhall.component.entity.room.dto.RoomWatermarkDTO;
import com.vhall.component.entity.room.vo.*;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.enums.OperateTypeEnum;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.plugin.common.support.Token;
import com.vhall.component.plugin.common.support.TokenThreadLocal;
import com.vhall.component.service.annotation.OperateLog;
import com.vhall.component.service.room.RoomConsoleService;
import com.vhall.component.service.room.RoomService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Map;

/**
 * @author guoxin.wang
 * @date 2021/7/12 17:30
 */
@Slf4j
@VhallComponent
public class RoomConsoleController implements RoomConsoleFacade {

    @Autowired
    private RoomService roomService;

    @Autowired
    private RoomConsoleService roomConsoleService;

    @Override
    @TokenAuth
    @OperateLog(module = "console-房间管理", type = OperateTypeEnum.CREATE, name = "创建")
    public RoomRespVO create(CreateRoomNewVO reqVO) {
        Preconditions.checkArgument(StringUtils.isNotBlank(reqVO.getImageUrl()), "直播封面不能为空！");
        return roomConsoleService.create(reqVO);
    }

    @Override
    @TokenAuth
    public Boolean submitForApproval(SubmitForApprovalVO vo) {
        return roomConsoleService.submitForApproval(vo);
    }

    @Override
    @TokenAuth
    public Boolean revokeReview(Integer ilId){
        return roomConsoleService.revokeReview(ilId);
    }

    @TokenAuth
    @Override
    public RoomConsoleListRespVO list(@Validated RoomConsoleListVO reqVO) {
        Token token = TokenThreadLocal.getToken();
        return roomConsoleService.list(token.getAccountId(), reqVO);
    }

    @TokenAuth
    @Override
    public RoomConsoleListRespVO complianceCheckList(RoomConsoleListVO reqVO) {
        Token token = TokenThreadLocal.getToken();
        return roomConsoleService.complianceCheckList(token.getAccountId(), reqVO);
    }

    @Override
    @TokenAuth
    public RoomConsoleListRespVO playbackCheck(RoomConsoleListVO reqVO) {
        return roomConsoleService.playbackCheck(reqVO);
    }

    @TokenAuth(check = false)
    @Override
    public List<InvestRefResponseVO> investRefList(Integer ilId) {
        return roomConsoleService.investRefList(ilId);
    }

    @TokenAuth
    @Override
    public RoomConsoleListRespVO roomAuditList(RoomAuditConsoleListVO reqVO) {
        return roomConsoleService.roomAuditList(reqVO);
    }

    @TokenAuth
    @Override
    public RoomSupplyRespVO getCompetence(RoomLimitsVO reqVO) {
        return roomConsoleService.getCompetence(reqVO);
    }


    @TokenAuth
    @Override
    public JSONObject getRoomStatus(RoomLimitsVO reqVO) {
        return roomConsoleService.getRoomStatus(reqVO);
    }

    @TokenAuth
    @Override
    public InavRoomGetRespVO getRoom(RoomLimitsVO reqVO) {
        // v4/room/console/get
        return roomConsoleService.getRoom(reqVO.getRoomId(), null);
    }

    @TokenAuth
    @Override
    public Boolean saveWarm(WarmVO reqVO) {
        if (reqVO.getType() != null && reqVO.getType() != 1 && reqVO.getType() != 0) {
            throw new BusinessException(BizErrorCode.BIZ_TYPE_NOT_ALLOWED);
        }
        // 校验如果type传递的是空串的话，因为我们后端的type是Integer，所以后端会设置成null.所以在这里进行设置。
        if (null == reqVO.getType()) {
            reqVO.setType(0);
        }
        Token accountInfo = TokenThreadLocal.getToken();
        return roomConsoleService.saveWarm(reqVO, accountInfo.getAccountId());
    }

    @TokenAuth
    @Override
    public WarmRespVO getWarmInfo( WarmReqVO reqVO) {
        return roomConsoleService.getWarmInfo(reqVO);
    }

    @TokenAuth
    @Override
    @OperateLog(module = "console-房间管理", type = OperateTypeEnum.UPDATE, name = "更新")
    public Boolean update( CreateRoomNewVO reqVO) {
        return roomConsoleService.update(reqVO);
    }

    @TokenAuth
    @Override
    public List<String> delete( DelRoomVO reqVO) {
        return roomService.delete(reqVO);
    }

    @TokenAuth
    @Override
    public JSONObject getStreamAddress( RoomLimitsVO reqVO) {
        return roomConsoleService.getStreamAddress(reqVO);
    }

    @TokenAuth
    @Override
    public JSONObject getAccessToken( RoomLimitsVO reqVO) {
        return roomConsoleService.getAccessToken(reqVO);
    }

    @Override
    @TokenAuth
    public Map<String, Object> getOnlineCheck( OnlineCheckVO reqVO) {
        return roomConsoleService.getOnlineCheck(reqVO);
    }

    @TokenAuth
    @Override
    public JSONObject customTag( CustomMenuVO reqVO) {
        return roomConsoleService.listCustomTag(reqVO);
    }

    @TokenAuth
    @Override
    public JSONObject editCustomTag( CustomMenuEditVO reqVO) {
        return roomConsoleService.editCustomTag(reqVO);
    }

    @TokenAuth
    @Override
    public Boolean addLiveMode( AddLiveModeVO reqVO) {
        if (!(reqVO.getLiveMode() == 1 || reqVO.getLiveMode() == 2)) {
            throw new BusinessException(BizErrorCode.COMP_TAG_LIVE_MODE_NOT_ALLOWED);
        }
        return roomConsoleService.addLiveMode(reqVO);
    }

    @TokenAuth
    @Override
    public Boolean addPushStream( PushStreamReqVO reqVO) {
        return roomConsoleService.addPushStream(reqVO);
    }

    @TokenAuth
    @Override
    public Boolean addPushStreamList(String data) {
        return roomConsoleService.addPushStreamList(data);
    }

    @TokenAuth
    @Override
    public Boolean delPushStreamsById(Integer id) {
        return roomConsoleService.delPushStreamsById(id);
    }

    @TokenAuth
    @Override
    public Boolean editPushStream( PushStreamReqVO reqVO) {
        return roomConsoleService.editPushStream(reqVO);
    }


    @TokenAuth
    @Override
    public PushStreamSingleRspVO getPushStreamDeatil(Integer id) {
        return roomConsoleService.getPushStreamDeatil(id);
    }

    @TokenAuth
    @Override
    public List<PushStreamSingleRspVO> getPushStreamList(PushStreamListReqVO reqVO) {
        return roomConsoleService.getPushStreamList(reqVO);

    }

    @TokenAuth
    @Override
    public Boolean pushStreamStart(Integer id) {
        return roomConsoleService.pushStreamStart(id);
    }

    @TokenAuth
    @Override
    public Boolean pushStreamStartBatch(String dataList) {
        return roomConsoleService.pushStreamStartBatch(dataList);
    }

    @TokenAuth
    @Override
    public Boolean pushStreamEnd(Integer id) {
        return roomConsoleService.pushStreamEnd(id);
    }

    @TokenAuth
    @Override
    public PushStreamStatisticsRspVO pushStreamStatistics(String roomId) {
        return roomConsoleService.pushStreamStatistics(roomId);
    }

    @TokenAuth
    @Override
    public Boolean createWatermark(RoomWatermarkVO roomWatermarkVO) {
        return roomConsoleService.watermarkSetting(roomWatermarkVO);
    }

    @TokenAuth
    @Override
    public RoomWatermarkDTO getRoomWatermark(String roomId) {
        return roomConsoleService.getRoomWatermark(roomId);
    }

    @Override
    public FirstFrameFromVideoRspVO getTheFirstFrameOfTheVideo(FirstFrameFromVideoVO reqVO) {
        return roomConsoleService.getTheFirstFrameOfTheVideo(reqVO);
    }

    @Override
    public Boolean reservationStatusRoom(RoomOperationStatusVO vo) {
        return roomConsoleService.reservationStatusRoom(vo);
    }

    @Override
    @TokenAuth
    public Boolean commentStatusRoom(RoomCommentStatusVO vo) {
        return roomConsoleService.commentStatusRoom(vo);
    }

    @Override
    public Boolean savePullStreamUrl(RoomPullStreamVO vo) {
        return roomConsoleService.savePullStreamUrl(vo);
    }

    @TokenAuth
    @Override
    public Boolean startPullStream(RoomPullStreamInfoVO vo) {
        return roomConsoleService.startPullStream(vo);
    }

    @TokenAuth
    @Override
    public Boolean endPullStream(RoomPullStreamInfoVO vo) {
        return roomConsoleService.endPullStream(vo);
    }
    @TokenAuth
    @Override
    public RoomPullStreamRespVO getPullStreamStatus(RoomPullStreamInfoVO vo) {
        return roomConsoleService.getPullStreamStatus(vo);
    }

    @Override
    public RoomBaseInfoResponseVO getRoomBaseInfo() {
        return roomConsoleService.getRoomBaseInfo();
    }

    @Override
    public List<LivePriceResponseVO> getLivePriceList() {
        return roomConsoleService.getLivePriceList();
    }

    @TokenAuth
    @Override
    public Boolean auditRoom(@Validated RoomAuditRequestVO vo) {
        return roomConsoleService.auditRoom(vo);
    }

    @Override
    public List<AuditRecordResponseVO> roomAuditRecordList(@Validated RoomAuditRecordRequestVO vo) {
        return roomConsoleService.roomAuditRecordList(vo);
    }

    @TokenAuth
    @OperateLog(module = "房间列表", type = OperateTypeEnum.UPDATE, name = "房间业务检查")
    @Override
    public Boolean inspectionRoom( RoomInspectionReqVO vo) {
        return roomConsoleService.inspectionRoom(vo);
    }

    @Override
    @TokenAuth
    public List<RoomQrCodeListRspVO> qrCodeList(String roomId) {
        return roomConsoleService.qrCodeList(roomId);
    }

    @Override
    @TokenAuth
    public Boolean genQrCode( RoomQrCodeListReqVO vo) {
        return roomConsoleService.genQrCode(vo);
    }

    @Override
    @TokenAuth
    public Boolean delQrCode(Integer id) {
        return roomConsoleService.delQrCode(id);
    }

    @TokenAuth
    @Override
    public Boolean consoleEndLive( ConsoleEndLiveRequestVO vo) {
        return roomConsoleService.consoleEndLive(vo);
    }

    @TokenAuth
    @Override
    public Boolean complianceCheck(Integer ilId, Integer auditStatus, String desc) {
        if (auditStatus == -1 && StrUtil.isEmpty(desc)){
            throw new BusinessException(60000, "请填写不通过的原因(100字以内)","");
        }
        return  roomConsoleService.complianceCheck(ilId, auditStatus, desc);
    }

    @Override
    public Long getVirtualPV(Integer ilId) {
        return roomService.getVirtualPV(ilId);
    }
    @TokenAuth
    @Override
    public LimitScopeWhiteListUploadResponseVO limitScopeWhiteListUpload(LimitScopeWhiteListUploadRequestVO vo) {
        return roomConsoleService.limitScopeWhiteListUpload(vo);
    }

    @Override
    public LiveNotNeedPlayBackResponseVO saveLiveNotNeedPlayBack(SaveLiveNotNeedPlayBackRequestVO vo) {
        return roomConsoleService.saveLiveNotNeedPlayBack(vo);
    }

    @Override
    public LiveNotNeedPlayBackResponseVO getLiveNotNeedPlayBack(RoomsBaseRequestVO vo) {
        return roomConsoleService.getLiveNotNeedPlayBack(vo);
    }
}
